import {
  Button,
  Form,
  FormItem,
  Input,
  message,
  Modal,
  Radio,
  RadioGroup,
  Space,
  TypographyText
} from 'ant-design-vue'
import { defineComponent, ref } from 'vue'

import { commonMenuApi } from '@/apis'
import { LabelItem } from '@/apis/commonMenyTypes'
import { type ProTableProps, type ProTableRef, ProTable } from '@/components'
import { TABLE_FORMAT, TABLE_ITEM_RENDER, TABLE_RENDERER } from '@/constants'
import { useDialog } from '@/hooks'

export default defineComponent({
  name: 'CLabelManage',
  setup() {
    const { showConfirm } = useDialog()
    const statusOptions = [
      { label: '禁用', value: 0 },
      { label: '启用', value: 1 }
    ]
    const tableRef = ref({} as ProTableRef)
    const showDialog = ref(false)
    const dialogLoading = ref(false)

    const optionItemCur = ref({} as LabelItem)
    const tableConfig = ref({
      fixedHeight: true,
      options: {
        border: true,
        align: 'center',
        toolbarConfig: {
          perfect: false,
          slots: {
            buttons: 'toolbarButtons'
          }
        },
        formConfig: {
          items: [
            {
              title: '标签名称',
              field: 'labelName',
              itemRender: {
                name: TABLE_ITEM_RENDER.AInput,
                props: {
                  placeholder: '请输入',
                  allowClear: true
                }
              }
            },
            {
              title: '状态',
              field: 'labelStatus',
              itemRender: {
                name: TABLE_ITEM_RENDER.ASelect,
                props: {
                  placeholder: '全部',
                  allowClear: true
                },
                options: statusOptions
              }
            },
            {
              itemRender: {
                name: TABLE_RENDERER.FormItemBtns
              }
            }
          ]
        },
        columns: [
          {
            title: '标签名称',
            field: 'labelName'
          },
          {
            title: '创建时间',
            field: 'createTime',
            formatter: [TABLE_FORMAT.Date, 'yyyy-MM-dd HH:mm:ss']
          },
          {
            title: '状态',
            fixed: 'right',
            width: 100,
            slots: {
              default: 'status'
            }
          },
          {
            title: '操作',
            fixed: 'right',
            slots: {
              default: 'operation'
            },
            width: 180
          }
        ],

        proxyConfig: {
          form: true,
          props: {
            result: 'data'
          },
          ajax: {
            query: async ({ form }) => {
              const data = await commonMenuApi.getLabelList({
                ...form
              })
              return data
            }
          }
        }
      }
    } as ProTableProps)

    //新增
    function add() {
      optionItemCur.value = { labelStatus: 1 } as LabelItem
      showDialog.value = true
    }

    //编辑
    function edit(item) {
      Object.assign(optionItemCur.value, item)
      showDialog.value = true
    }

    //编辑/新增确定
    async function dialogConfirm() {
      if (!optionItemCur.value.labelName) {
        message.error('请输入标签名称')
        return
      }

      try {
        dialogLoading.value = true
        if (optionItemCur.value.id) {
          await commonMenuApi.editLabel(optionItemCur.value)
        } else {
          await commonMenuApi.addLabel(optionItemCur.value)
        }
        showDialog.value = false
        tableRef.value.refresh()
      } finally {
        dialogLoading.value = false
      }
    }

    //启用禁用操作
    function handleForbidden(item: LabelItem) {
      showConfirm({
        content:
          item.labelStatus == 1
            ? '确认禁用该运费模板？'
            : '确定启用该运费模板？',
        onOk: async () => {
          try {
            await commonMenuApi.upDownLabel(
              item.id,
              item.labelStatus == 1 ? 0 : 1
            )
            tableRef.value.refresh()
          } catch (error) {}
        }
      })
    }

    return {
      tableRef,
      tableConfig,
      showDialog,
      optionItemCur,
      dialogConfirm,
      add,
      edit,
      handleForbidden
    }
  },
  render() {
    return (
      <div>
        <ProTable
          ref="tableRef"
          v-slots={{
            status: ({ row }) => {
              return (
                <Space>
                  {row.labelStatus == 0 ? (
                    <TypographyText type="danger">禁用</TypographyText>
                  ) : (
                    <TypographyText type="success">启用</TypographyText>
                  )}
                </Space>
              )
            },
            operation: ({ row }) => (
              <Space>
                <Button
                  type="link"
                  size="small"
                  onClick={() => this.handleForbidden(row)}
                >
                  {row.labelStatus == 1 ? '禁用' : '启用'}
                </Button>
                <Button type="link" size="small" onClick={() => this.edit(row)}>
                  编辑
                </Button>
              </Space>
            ),
            toolbarButtons: () => {
              return (
                <Space>
                  <Button type="primary" onClick={this.add}>
                    新增标签
                  </Button>
                </Space>
              )
            }
          }}
          {...this.tableConfig}
        />
        <Modal
          v-model:visible={this.showDialog}
          title="新增"
          destroyOnClose
          width={500}
          onOk={this.dialogConfirm}
        >
          <Form labelCol={{ span: 5 }}>
            <FormItem label={'标签名称'} required>
              <Input
                maxlength={60}
                v-model:value={this.optionItemCur.labelName}
                placeholder="请输入"
                style={{ width: '300px' }}
              />
            </FormItem>
            <FormItem label="状态：" required>
              <RadioGroup v-model:value={this.optionItemCur.labelStatus}>
                <Radio value={1}>启用</Radio>
                <Radio value={0}>禁用</Radio>
              </RadioGroup>
            </FormItem>
          </Form>
        </Modal>
      </div>
    )
  }
})
